(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("signonService", signonService);
	signonService.$inject = [ '$rootScope', '$q', '$locale', 'Constants', 'loggerFactory', 'storageService', 'ajaxServiceFactory', 'SYS_PARAMS'];
	function signonService($rootScope, $q, $locale, Constants, loggerFactory, storageService, ajaxServiceFactory, SYS_PARAMS) {
		var log = loggerFactory.getLogger("signonService"), ajaxService = ajaxServiceFactory.buildAjaxService({
			loadMenus : {
				url : 'do/menu',
				method : 'get'
			},
			loadUserInfo : {
				url : 'do/user/info',
				method : 'get'
			},
			exit : {
				url : 'do/p/signoff',
				method : 'get'
			},
			getUserStatus : {
				url : 'do/cda/dataList/userStatus',
				method : 'get'
			},
			getLocaleList : {
				url : 'do/cda/dataList/locales',
				method : 'get'
			},
			getSiteCurrencyConfig : {
				url : 'do/cda/dataList/getSiteCurrencyConfig',
				method : 'get'
			}
		});
		function getUserStatus() {
			var defer = $q.defer();
			ajaxService.getUserStatus().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getLocaleList() {
			var defer = $q.defer();
			ajaxService.getLocaleList().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getSiteCurrencyConfig() {
			var defer = $q.defer();
			ajaxService.getSiteCurrencyConfig().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function isAuthenticated() {
			var defer = $q.defer();
			ajaxService.loadUserInfo().then(function(response) {
				if (storageUserInfo(response)) {
					ajaxService.loadMenus().then(function(menus) {
						storageService.saveMenuInfo(menus.data ? menus.data[1] || [] : []);
						storageService.saveMenuConfigInfo(menus.data ? menus.data[0] || [] : []);
						getUserStatus().then(function(response) {
							if(response && response.data){
								angular.forEach(response.data, function(data, index, array){
									SYS_PARAMS.USER_STATUS[data.value.toUpperCase()] = data.label;
								});
							}
						});

						getLocaleList().then(function(response) {
							if(response && response.data){
								angular.forEach(response.data, function(data, index, array){
									SYS_PARAMS.LOCALE_STATUS[data.value.toUpperCase()] = data.label;
								});
							}
						});
						
						getSiteCurrencyConfig().then(function(response) {
							if(response && response.data){
								angular.forEach(response.data, function(data, index, array){
									SYS_PARAMS.CCY_CONFIG[data.ccy] = data.decimalNum;
								});
							}
						});
						defer.resolve();
					}, function(error) {
						log.debug(angular.toJson(error));
						defer.reject('load menu info error');
					});
				} else {
					defer.reject();
				}
			}, function() {
				defer.reject();
			});
			return defer.promise;
		}

		function exit() {
			storageService.clearAllFromSession();
			// storageService.clearAllFromLocal();
			delete $rootScope.user;
			delete $rootScope.site;
			delete $rootScope.menus;
			return ajaxService.exit();
		}

		function afterSignon(response) {
			var defer = $q.defer();
			storageService.clearAllFromSession();
			// storageService.clearAllFromLocal();
			delete $rootScope.user;
			delete $rootScope.site;
			delete $rootScope.menus;
			if (response && response.data) {
				if (storageUserInfo(response)) {
					ajaxService.loadMenus().then(function(menus) {
						storageService.saveMenuInfo(menus.data ? menus.data[1] || [] : []);
						storageService.saveMenuConfigInfo(menus.data ? menus.data[0] || [] : []);
						defer.resolve();
					}, function(error) {
						log.debug(angular.toJson(error));
						defer.reject('load menu info error');
					});
				} else {
					defer.reject('load user info error');
				}
			} else {
				ajaxService.loadUserInfo().then(function(response) {
					if (storageUserInfo(response)) {
						ajaxService.loadMenus().then(function(menus) {
							storageService.saveMenuInfo(menus.data ? menus.data[1] || [] : []);
							storageService.saveMenuConfigInfo(menus.data ? menus.data[0] || [] : []);
							defer.resolve();
						}, function(error) {
							log.debug(angular.toJson(error));
							defer.reject('load menu info error');
						});
					} else {
						defer.reject('load user info error');
					}
				}, function() {
					defer.reject('load user info error');
				});
			}
			return defer.promise;
		}

		function storageUserInfo(response) {
			var siteInfo, userInfo, formatInfo, systemInfo;
			if (!response || !response.data || !angular.isObject(response.data)) {
				return false;
			}
			systemInfo = response.data.systemInfo;
			formatInfo = response.data.formatInfo;
			userInfo = response.data.userInfo;
			siteInfo = response.data.siteInfo;
			if (formatInfo) {
				angular.forEach(formatInfo, function(value, key) {
					if (angular.isString(value)) {
						formatInfo[key] = value.replace('SSS', 'sss');
					}
				});
				storageService.saveFormatInfo(formatInfo);
				angular.extend($locale.DATETIME_FORMATS, formatInfo);
			}
			if (systemInfo) {
				Constants.ENCODE_SEPARATOR = systemInfo.ENCODE_SEPARATOR;
				Constants.CSRF_TOKEN = systemInfo.CSRF_TOKEN;
				Constants.TRANSACTION_TOKEN = systemInfo.TRANSACTION_TOKEN;
			}
			storageService.saveSiteInfo(siteInfo);
			storageService.saveUserInfo(userInfo);
			return true;
		}
		return {
			afterSignon : afterSignon,
			isAuthenticated : isAuthenticated,
			exit : exit
		};
	}
})(angular);